﻿using System;
using System.Collections.Generic;
using FairyGUI;
using UnityEngine;

public class UI_ServerList : Window
{
    public GButton m_btnBack;
    public GList m_zoneList;
    public GList m_serverList;
    public int m_page = 0;
    protected override void OnInit()
    {
        base.OnInit();
        this.contentPane = UIManager.Instance.LoadComponent("Login", "UIServerList");
        m_btnBack = (GButton)this.contentPane.GetChild("btnBack");
        m_zoneList = (GList)this.contentPane.GetChild("zoneList");
        m_serverList = (GList)this.contentPane.GetChild("serverList");
        //返回
        m_btnBack.onClick.Add(() =>
        {
            this.Hide();
            UIManager.Instance.ShowWindow<UI_ServerSelect>();
        });
        m_zoneList.itemRenderer = ZoneRenderer;
        m_zoneList.onClickItem.Add(ZoneClick);
        m_zoneList.numItems = ServerMgr.Instance.GetMaxPage(10);
        m_zoneList.selectedIndex = 0;

        m_serverList.itemRenderer = ServerRenderer;
        m_serverList.onClickItem.Add(ServerClick);
        m_serverList.SetVirtual();
        m_serverList.numItems = 10;
    }

    public void InitUIData()
    {
        int lastServerId = AccountMgr.Instance.lastServerId;
        if (lastServerId == 0)
            return;
        ServerItem server = ServerMgr.Instance.GetServer(lastServerId);
        if (server == null)
            return;
        ((GTextField)this.contentPane.GetChild("lastServer")).text = GetServerUIName(server.id + 1, server.name);
        this.contentPane.GetController("lastServerStatus").selectedIndex = (int)server.status;
        ((GTextField)this.contentPane.GetChild("zoneInfo")).text = GetZoneUIName(1, 10);
    }

    string GetServerUIName(int id, string name)
    {
        return string.Format("{0}区     {1}", id, name);
    }

    string GetZoneUIName(int startId, int endId)
    {
        return string.Format("服务器 {0}-{1}", startId, endId);
    }

    protected override void OnShown()
    {
        base.OnShown();
        InitUIData();
    }

    private void ZoneRenderer(int index, GObject obj)
    {
        int page = index * 10 + 1;
        obj.text = string.Format("{0} - {1}区", index * 10 + 1, Mathf.Min(index * 10 + 10, ServerMgr.Instance.GetCount()));
        GComponent component = obj.asCom;
        component.data = page;
    }

    private void ServerRenderer(int index, GObject obj)
    {
        int id = m_page * 10 + index;
        ServerItem item = ServerMgr.Instance.GetServer(id);
        Debug.LogFormat("page:{0} id:{1} index:{2}", m_page, id, index);
        if (item == null)
            return;
        obj.text = GetServerUIName(id+1, item.name);
        GComponent component = obj.asCom;
        component.GetController("status").selectedIndex = (int)item.status;
        component.GetController("new").selectedIndex = item.isNew ? 1 : 0;
        component.data = id;
    }

    private void ZoneClick(EventContext context)
    {
        GComponent component = context.data as GComponent;
        int page = (int)component.data;
        m_page = (int)(page / 10);
        m_serverList.numItems = 0;
        m_serverList.numItems = Math.Min(ServerMgr.Instance.GetCount() - page, 10);
        int startId = m_page * 10 + 1;
        int endId = startId +  Math.Min(ServerMgr.Instance.GetCount() - page, 10) - 1;
        ((GTextField)this.contentPane.GetChild("zoneInfo")).text = GetZoneUIName(startId, endId);
        Debug.LogFormat("zoneClick context:{0} page:{1} m_page:{2}", (context.data as GObject).text, page, m_page);
    }


    private void ServerClick(EventContext context)
    {
        GComponent component = context.data as GComponent;
        int id = (int)component.data;
        AccountMgr.Instance.LoginServer(id);
        this.Hide();
        UIManager.Instance.ShowWindow<UI_ServerSelect>();
        Debug.LogFormat("serverClick context:{0} id:{1}", (context.data as GObject).text, id);
    }
}